package model

import (
	"time"

	"gorm.io/gorm"
)

type User struct {
	UserId      int64     `gorm:"column:user_id;primaryKey;autoIncrement;" json:"userId"`
	DeptId      int64     `gorm:"column:dept_id;" json:"deptId"`
	Dept        Dept      `gorm:"foreignKey:DeptId;references:DeptId" json:"dept"`
	UserName    string    `gorm:"column:user_name;unique" json:"userName"`
	NickName    string    `gorm:"column:nick_name;" json:"nickName"`
	UserType    string    `gorm:"column:user_type;default:01" json:"userType"`
	Email       string    `gorm:"column:email;" json:"email"`
	Phonenumber string    `gorm:"column:phonenumber;" json:"phonenumber"`
	Sex         string    `gorm:"column:sex;default:2" json:"sex"`
	Avatar      string    `gorm:"column:avatar;" json:"avatar"`
	Password    string    `gorm:"column:password;" json:"password"`
	LoginIp     string    `gorm:"column:login_ip;" json:"loginIp"`
	LoginDate   time.Time `gorm:"column:login_date;" json:"loginDate"`
	Roles       []Role    `gorm:"many2many:sys_user_role;joinForeignKey:user_id;joinReferences:role_id" json:"roles"`
	RoleIDs     []int64   `gorm:"-"`
	Posts       []Post    `gorm:"many2many:sys_user_post;joinForeignKey:user_id;joinReferences:post_id" json:"posts"`
	PostIds     []int64   `gorm:"-"`
	Common      `gorm:"embedded"`
}

func (User) TableName() string {
	return "sys_user"
}

func (User) BeforeCreate(tx *gorm.DB) (err error) {
	// user.Remark = "我是备注"
	return nil
}
